"""
Case Type   : 资源池化集群DMS reform优化
Case Name   : kill主节点进程->reform在flush_copy阶段kill备1、备2进程
Create At   : 2024/05/13
Owner       : @kyleze
Description :
    1.kill主机进程
    2.持续观察DMS日志
    3.kill备1、备2进程
    4.查询集群状态
Expect      :
    1.成功
    2.reform进入flush_copy阶段
    3.成功
    4.集群恢复正常
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


class DmsRform(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.pri_user_node = Node(node="PrimaryDbUser")
        self.st1_user_node = Node(node="Standby1DbUser")
        self.st2_user_node = Node(node="Standby2DbUser")
        self.dms_log_file = os.path.join('$GAUSSLOG', 'pg_log/DMS/run/dms.rlog')
        self.reform_start = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};grep "dms_reform_set_start" '
            f'{self.dms_log_file}|wc -l').result())
        self.flush_copy = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};'
            f'grep "dms_reform_flush_copy_parallel enter" '
            f'{self.dms_log_file}|wc -l').result())

    def test_dms(self):
        step = '--step1:kill主节点进程 expect:成功--'
        self.log.info(step)
        kill_cmd = "ps ux | grep '\-D' | grep gaussdb | grep -v grep | " \
                   "awk '{print $2}' | xargs kill -9"
        kill_res = self.pri_user_node.sh(kill_cmd).result()
        self.log.info(kill_res)
        self.assertIn('', kill_res, "执行失败" + step)

        step = '--step2:持续观察DMS日志 expect:reform进入flush_copy阶段--'
        self.log.info(step)
        reform_flush_num = 0
        for i in range(10):
            reform_start_num = int(
                self.pri_user_node.sh(f'grep "dms_reform_set_start" '
                                      f'{self.dms_log_file}|wc -l').result())
            reform_flush_num = int(self.pri_user_node.sh(
                f'grep "dms_reform_flush_copy_parallel enter" '
                f'{self.dms_log_file}|wc -l').result())
            if reform_start_num > self.reform_start \
                    and reform_flush_num > self.flush_copy:
                break
        self.assertTrue(reform_flush_num > self.flush_copy, "执行失败" + step)

        step = '--step3:kill备1、备2进程 expect:成功--'
        self.log.info(step)
        st1_kill_res = self.st1_user_node.sh(kill_cmd).result()
        self.log.info(st1_kill_res)
        self.assertIn('', st1_kill_res, "执行失败" + step)
        st2_kill_res = self.st2_user_node.sh(kill_cmd).result()
        self.log.info(st2_kill_res)
        self.assertIn('', st2_kill_res, "执行失败" + step)

        step = '--step4:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        time.sleep(20)
        status = self.primary_sh.get_db_cluster_status('status')
        self.assertTrue("Normal" in status, "执行失败" + step)

    def tearDown(self):
        self.log.info(f'----{os.path.dirname(__file__)} end----')
